
<!DOCTYPE html>
<html lang="en" class="loading">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>关于Mybatis打印日志部分的相关优化 - kyralo</title>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate">
    <meta name="keywords" content="kyralo,"> 
    <meta name="description" content="一.导入jar包​    要打印日志必须要有log4j包.我这里用的是log4j-1.2.17.jar
二.写 log4j.properties我这里有一个大致的模板​
log4j.rootCate,"> 
    <meta name="author" content="kyralo"> 
    
    <link rel="icon" href="https://cdn.jsdelivr.net/gh/kyralo/cdn/ico/favicon.ico"> 
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kyralo/cdn@v1.0.2/css/diaspora.css">
    <!-- <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> -->
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <!-- <script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script> -->
</head>
</html>
<body class="loading">
    <span id="config-title" style="display:none">kyralo</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="icon-home image-icon" href="javascript:;" data-url="https://kyralo.github.io"></a>
    <!-- <div title="播放/暂停" class="icon-play"></div> -->
    <h3 class="subtitle">关于Mybatis打印日志部分的相关优化</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">关于Mybatis打印日志部分的相关优化</h1>
        <div class="stuff">
            <span>五月 20, 2019</span>
            
  <ul class="post-tags-list"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/Java/">Java</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/Mybatis/">Mybatis</a></li></ul>


        </div>
        <div class="content markdown">
            <h3 id="一-导入jar包"><a href="#一-导入jar包" class="headerlink" title="一.导入jar包"></a>一.导入jar包</h3><p>​    要打印日志必须要有log4j包.我这里用的是log4j-1.2.17.jar</p>
<h3 id="二-写-log4j-properties"><a href="#二-写-log4j-properties" class="headerlink" title="二.写 log4j.properties"></a>二.写 log4j.properties</h3><p>我这里有一个大致的模板​</p>
<pre><code>log4j.rootCategory=ERROR,CONSOLE,LOGFILE
#fatal（致命错误）&gt;error（错误）&gt;warn（警告）&gt;info（普通信息）&gt;debug（调试信息）

log4j.logger.online.wzeal.Mapper.Flowermapper=DEBUG

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{YYYY-MM-DD hh:mm:ss}  %C   %L  %m   %n 

log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=src/Mybatis.log
log4j.appender.LOGFILE.Append=false
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern= %d{YYYY-MM-DD hh:mm:ss}  %C   %L  %m   %n</code></pre><p>​    现在我参照模板一步一步说明</p>
<p> 1.首先第一行log4j.rootCategory =的意思是设置输出的位置即输出内容的级别。</p>
<p>​    即ERROR 表示 输出程序运行过程中级别为error和在error以上的信息，</p>
<p>​    各日志级别对应如下：</p>
<p>fatal（致命错误）&gt;error（错误）&gt;warn（警告）&gt;info（普通信息）&gt;debug（调试信息）    </p>
<p>​    这样我们可以直接通过代码打印日志到控制台</p>
<pre><code>import org.apache.log4j.Logger;

public class test2 {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(test2.class);
        logger.debug(&quot;这是一个调试信息&quot;);
        logger.info(&quot;这是一个普通信息&quot;);
    }
}</code></pre><p>其中方法名对应日志级别</p>
<p>​    CONSOLE 表示在控制台输出优化后的日志信息</p>
<p>​    LOGFILE      表示将优化后的日志信息写入文本中</p>
<p>2.接下来 log4j.logger.online.wzeal.Mapper.Flowermapper=DEBUG 表示特定输出运行到online.wzeal.Mapper.Flowermapper时级别为debug的日志信息（这一步很关键，是优化的关键）</p>
<p>3.然后log4j.appender.CONSOLE=  是对输出到控制台的相关信息的设置，我们主要看到</p>
<p>log4j.appender.CONSOLE.layout.ConversionPattern=%d{YYYY-MM-DD hh:mm:ss}  %C   %L  %m   %n</p>
<p>这里是设置要输出什么到控制台，   %d{YYYY-MM-DD hh:mm:ss}  ：表示格式为{年-月-日 时-分-秒}日期</p>
<p>%C ： 表示使用的类的路径（包名+类名） </p>
<p>%L： 表示第几行</p>
<p>%m：表示输出程序执行信息   </p>
<p>%n：表示换行</p>
<p>更多其他设置需要访问官方文档</p>
<p>4.接下来log4j.appender.LOGFILE= 是输入到文本的内容进行限制</p>
<p>其中log4j.appender.LOGFILE.layout.ConversionPattern= %d{YYYY-MM-DD hh:mm:ss}  %C   %L  %m   %n</p>
<p>这部分跟以上第3点中差不多的，这里就不再说了。</p>
<p>主要看到log4j.appender.LOGFILE.File=src/Mybatis.log   </p>
<p>这个地方设置输入日志的文本文件的存放位置，我这里设置的是在src目录下</p>
<p>（如果该目录下没有文件名为Mybatis.log  的文件，那么当程序运行时会自动新建一个Mybatis.log  文件）</p>
<h3 id="三-日志优化的作用"><a href="#三-日志优化的作用" class="headerlink" title="三.日志优化的作用"></a>三.日志优化的作用</h3><p>在说作用之前先看一下日志优化的效果</p>
<p><img src="https://view.moezx.cc/images/2019/08/02/4-1.md.png" alt></p>
<p>从效果图中我们可以很清楚的看到软件执行过程中的相关信息，并且过滤了很多不必要的信息</p>
<p>这样我们在开发中可以很清楚的了解软件的运行状况，并且若出错也会非常容易找到错误</p>

        </div>
        
    <div id='gitalk-container' class="comment link"
        data-ae='false'
        data-ci='eeec43124e53633945c0'
        data-cs='e2332cffb30ae24f185c1701286958cddd748ffb'
        data-r='kyralo.github.io'
        data-o='kyralo'
        data-a='kyralo'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
            <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#一-导入jar包"><span class="toc-number">1.</span> <span class="toc-text">一.导入jar包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二-写-log4j-properties"><span class="toc-number">2.</span> <span class="toc-text">二.写 log4j.properties</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三-日志优化的作用"><span class="toc-number">3.</span> <span class="toc-text">三.日志优化的作用</span></a></li></ol>
        </div>
    
</div>


    </div>
</div>
</body>
<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/plugin.js"></script>
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/diaspora.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/kyralo/cdn/css/photoswipe.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/kyralo/cdn/css/default-skin.css">
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/photoswipe.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/photoswipe-ui-default.min.js"></script>

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<!-- Google Analytics -->
<script type="text/javascript">
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-146789315-1', 'auto');
  ga('send', 'pageview');
</script>
<!-- End Google Analytics -->


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10/dist/APlayer.min.css">
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10/dist/APlayer.min.js"></script>
<style>
  .aplayer .aplayer-lrc {
    height: 35px;
  }
  .aplayer .aplayer-lrc p{
    font-size: 16px;
    font-weight: 700;
    line-height: 18px !important;
  }
  .aplayer .aplayer-lrc p.aplayer-lrc-current{
    color: #FF1493;
  }
  .aplayer.aplayer-narrow .aplayer-body{
    left: -66px !important;
  }
  .aplayer.aplayer-fixed .aplayer-lrc {
    display: none;
  }
  .aplayer .aplayer-lrc.aplayer-lrc-hide {
      display:none !important;
  }
  .aplayer.aplayer-fixed .lrc-show {
    display: block;
    background: rgba(255, 255, 255, 0.8);
  }
</style>
<div class="aplayer"

    data-id="2725346449"

    data-server="netease"

    data-type="playlist"

    data-fixed="true"

    data-autoplay="false"

    data-loop="all"

    data-order="random"

    data-preload="auto"

    data-volume="0.7"

    data-mutex="true"

</div>
<script src="https://cdn.jsdelivr.net/npm/meting@1.2/dist/Meting.min.js"></script>
<script>
  $(function(){
    $('body').on('click', '.aplayer', function(){
      if($('.aplayer-button').hasClass('aplayer-play')) {
        $('.aplayer-lrc').removeClass('lrc-show');
      } else {
        $('.aplayer-lrc').addClass('lrc-show');
      }
    })
  });
</script>
</html>
